package com.um.jdy.platform.activity.manager;

import cn.hutool.core.bean.BeanUtil;
import com.um.jdy.common.activity.entity.po.Discounts;
import com.um.jdy.common.activity.entity.po.DiscountsStation;
import com.um.jdy.common.activity.entity.vo.DiscountsVO;
import com.um.jdy.common.activity.service.DiscountsService;
import com.um.jdy.common.activity.service.DiscountsStationService;
import com.um.jdy.platform.activity.entity.dto.SaveDiscountsDTO;
import com.um.jdy.platform.activity.entity.enums.ActivityEnum;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.orm.params.PageParams;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 管理类：满减管理
 */
@Component
public class DiscountsManager extends BaseManager<DiscountsService> {

    @Autowired
    DiscountsStationService discountsStationService;

    public Page<Discounts> getPage(PageParams pageParams){
        Page<Discounts> page = new Page<>();
        page.setPageNo(pageParams.getPageNo());
        page.setPageSize(pageParams.getPageSize());

        pageParams.getCondition().put("com_id",getComId());

        page = service.getPage(page,pageParams.getProperties(),pageParams.getValues(),
                pageParams.getRangeEntitys(),pageParams.getOrderNames(),pageParams.getSortOns());
        return page;
    }

    public DiscountsVO get(String id) {
        Discounts discounts = service.getEntity( id);
        return UMBeanUtils.copyBean(discounts,DiscountsVO.class);
    }

    public void addDiscounts(SaveDiscountsDTO saveDiscountsDTO) {
        Discounts discounts = BeanUtil.toBean(saveDiscountsDTO.getDiscounts(), Discounts.class);
        discounts.setCreateTime(new Date());
        if(ActivityEnum.DiscountsType.Discount.name().equals(discounts.getType())){
            discounts.setPrice(0);
        }else{
            discounts.setScale(100);
        }
        service.save(discounts);

        if(ActivityEnum.UseRange.Part.name().equals(discounts.getAvailableStation())) {
            saveDiscountsDTO.getStationIds().forEach(stationId -> {
                DiscountsStation discountsStation = new DiscountsStation();
                discountsStation.setActivityDiscountsId(discounts.getActivityDiscountsId());
                discountsStation.setDeviceStationId(stationId);
                discountsStationService.save(discountsStation);
            });
        }
    }


    public void editDiscounts(String activityDiscountsId, SaveDiscountsDTO saveDiscountsDTO) {
        Discounts discounts = BeanUtil.toBean(saveDiscountsDTO.getDiscounts(), Discounts.class);
        discounts.setActivityDiscountsId(activityDiscountsId);
        if(ActivityEnum.DiscountsType.Discount.name().equals(discounts.getType())){
            discounts.setPrice(0);
        }else{
            discounts.setScale(100);
        }
        service.save(discounts);

        discountsStationService.deleteTrue("activity_discounts_id", activityDiscountsId);

        if(ActivityEnum.UseRange.Part.name().equals(discounts.getAvailableStation())) {
            saveDiscountsDTO.getStationIds().forEach(stationId -> {
                DiscountsStation discountsStation = new DiscountsStation();
                discountsStation.setActivityDiscountsId(activityDiscountsId);
                discountsStation.setDeviceStationId(stationId);
                discountsStationService.save(discountsStation);
            });
        }

    }

    @Transactional
    public Response delete(String id) {
        boolean result = service.delete(id);
        discountsStationService.delete("activity_discounts_id",id);
        return result ? Response.success() : Response.failed();
    }

    @Transactional
    public Response deleteIds(List<String> ids) {
        for(String id : ids){
            discountsStationService.delete("activity_discounts_id",id);
        }
        boolean result = service.deleteIds(ids);
        return result ? Response.success() : Response.failed();
    }

    public DiscountsVO editStatus(String id, String status) {
        Discounts discounts = new Discounts();
        discounts.setActivityDiscountsId(id);
        discounts.setStatus(status);
        service.update(discounts);
        return BeanUtil.toBean(discounts, DiscountsVO.class);
    }
}
